package com.kotlin.demo.构造函数的用法

import com.kotlin.demo.responseResult

//柱构造函数
class KtDemo(var name: String = "zq", val age: Int = 12) {

    //lateinit:使用之前要手动初始化 ,否则会报错
    lateinit var responseResultInfo: String

    // by lazy 惰性初始化:使用时候,自动初始化
    val dataBaseData  =  readSqlServerDataAction()  //类加载的时候,会初始化 dataBaseData 同时调用了 readSqlServerDataAction 方法

    fun readSqlServerDataAction() : String {
        for (index in 1..15){
            println("readSqlServerDataAction:初始化数据")
        }
        return "初始化成功"
    }

    fun readSqlServerDataAction1() : String {
        for (index in 1..15){
            println("readSqlServerDataAction1:初始化数据")
        }
        return "初始化成功"
    }

    val dataBaseData1 by lazy { readSqlServerDataAction1() }


    //模拟服务器请求
    fun request() {
        responseResultInfo = ""
    }

    fun showResponseResult() {
        if (::responseResultInfo.isInitialized){
            println("responseResultInfo:$responseResultInfo")
        }else{
            println("还没有初始化")
        }
    }

    /**
     * constructor must call base constructor ,
     */
    constructor(name: String = "luoLin") : this(name, 23) {
        println("次构造参数,")
    }

    constructor() : this("", 12) {
        println("次构造空参构造")
    }

    constructor(name: String = "zhuning", age: Int = 23, info: String = "oinif") : this() {
        println("次构造函数调用次构造函数")  //
    }

    //默认参数
    fun show() {
        println(name)
        println(age)
    }

}


fun main() {
    KtDemo("zq", 23).show()
    val ktDemo = KtDemo() //如果所有的参数都有默认值,那么默认调用主构造函数
    ktDemo.request()
    ktDemo.showResponseResult()
    //判断是否初始化
    Thread.sleep(3000)
    ktDemo.dataBaseData1

}